home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / pmake / customs / importquota.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  5.2 KB  |  182 lines

  1. /*-
  2.  * importquota.c --
  3.  *    A program to set the availability of the local host.
  4.  *
  5.  * Copyright (c) 1988, 1989 by the Regents of the University of California
  6.  * Copyright (c) 1988, 1989 by Adam de Boor
  7.  * Copyright (c) 1989 by Berkeley Softworks
  8.  *
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any non-commercial purpose
  11.  * and without fee is hereby granted, provided that the above copyright
  12.  * notice appears in all copies.  The University of California,
  13.  * Berkeley Softworks and Adam de Boor make no representations about
  14.  * the suitability of this software for any purpose.  It is provided
  15.  * "as is" without express or implied warranty.
  16.  */
  17. #ifndef lint
  18. static char *rcsid =
  19. "$Id: importquota.c,v 1.7 89/11/14 13:46:07 adam Exp $ SPRITE (Berkeley)";
  20. #endif lint
  21.  
  22. #include    "customs.h"
  23. #include    <sys/time.h>
  24. #include    <stdio.h>
  25.  
  26. /*-
  27.  *-----------------------------------------------------------------------
  28.  * gettime --
  29.  *    Get a time value from a string. It must be in the form
  30.  *        mm:ss
  31.  *    where 'm' is a minute's digit and 's' is a second's degit.
  32.  *    neither number may be greater than 60, for obvious reasons.
  33.  *
  34.  * Results:
  35.  *
  36.  * Side Effects:
  37.  *
  38.  *-----------------------------------------------------------------------
  39.  */
  40. int
  41. gettime (str)
  42.     char    *str;
  43. {
  44.     int        min,
  45.         sec;
  46.  
  47.     for (min = 0;
  48.      *str != '\0' && *str != ':' ;
  49.      min = 10 * min + *str++ - '0') {
  50.          continue;
  51.     }
  52.     if (*str == '\0') {
  53.     sec = min;
  54.     min = 0;
  55.     } else {
  56.     for (sec = 0, str++; *str != '\0'; sec = 10 * sec + *str++ - '0') {
  57.         continue;
  58.     }
  59.     }
  60.     if (min >= 60 || sec >= 60) {
  61.     fprintf (stderr, "malformed time\n");
  62.     exit(1);
  63.     }
  64.     return (min * 60 + sec);
  65. }
  66.  
  67. /*-
  68.  *-----------------------------------------------------------------------
  69.  * main --
  70.  *    Usage:
  71.  *        importquota -check mm:ss -idle mm:ss -swap pct -load float
  72.  *
  73.  *    -check          interval at which to check availability
  74.  *    -idle          minimum time the keyboard must be idle for the
  75.  *                  host to be available
  76.  *    -swap          minimum percentage of swap space that must
  77.  *                  be free for the host to be available
  78.  *    -load          a floating-point number which is the maximum
  79.  *                  load average the host can have before it becomes
  80.  *                  unavailable.
  81.  *    -jobs          maximum number of imported jobs allowed at once
  82.  *
  83.  *    The -idle, -swap and -load have certain limits (e.g. you cannot
  84.  *    say -idle 400:00). To turn off the checking of any criterion,
  85.  *    give a 0 value for it (e.g. -load 0 means not to worry about the
  86.  *    load averages on the machine).
  87.  *
  88.  * Results:
  89.  *    None, really.
  90.  *
  91.  * Side Effects:
  92.  *    The criteria are altered if all are within bounds.
  93.  *
  94.  *-----------------------------------------------------------------------
  95.  */
  96. main (argc, argv)
  97.     int        argc;
  98.     char    **argv;
  99. {
  100.     Avail_Data          criteria,
  101.             current;
  102.     struct timeval    interval;
  103.     double            maxLoad,
  104.             atof();
  105.     Rpc_Stat             rstat;
  106.     Boolean           beNice = -1;
  107.     
  108.     interval.tv_sec = interval.tv_usec = 0;
  109.     criteria.changeMask = 0;
  110.  
  111.     for (argc--, argv++; argc > 1; argc -= 2, argv += 2) {
  112.     if (strcmp (*argv, "-check") == 0) {
  113.         interval.tv_sec = gettime (argv[1]);
  114.     } else if (strcmp (*argv, "-idle") == 0) {
  115.         criteria.idleTime = gettime (argv[1]);
  116.         criteria.changeMask |= AVAIL_IDLE;
  117.     } else if (strcmp (*argv, "-swap") == 0) {
  118.         criteria.swapPct = atoi (argv[1]);
  119.         criteria.changeMask |= AVAIL_SWAP;
  120.     } else if (strcmp (*argv, "-load") == 0) {
  121.         maxLoad = atof (argv[1]);
  122.         criteria.loadAvg = (int)(maxLoad * LOADSCALE);
  123.         criteria.changeMask |= AVAIL_LOAD;
  124.     } else if (strcmp (*argv, "-jobs") == 0) {
  125.         criteria.imports = atoi(argv[1]);
  126.         criteria.changeMask |= AVAIL_IMPORTS;
  127.     } else {
  128.         fprintf (stderr, "Unknown flag: %s\n", *argv);
  129.         exit(1);
  130.     }
  131.     }
  132.  
  133.     if (interval.tv_sec) {
  134.     rstat = Customs_AvailInterval(&interval);
  135.     if (rstat != RPC_SUCCESS) {
  136.         Customs_PError("Customs_AvailInterval\n");
  137.         fprintf(stderr, "Could not change availability interval\n");
  138.     }
  139.     }
  140.  
  141.     rstat = Customs_SetAvail(&criteria);
  142.  
  143.     if (rstat != RPC_SUCCESS) {
  144.     Customs_PError("Customs_SetAvail");
  145.     fprintf (stderr, "Could not change criteria\n");
  146.     } else if (criteria.changeMask) {
  147.     fprintf (stderr, "Values out of range:\n");
  148.     if (criteria.changeMask & AVAIL_IDLE) {
  149.         fprintf (stderr, "\tidle time too long (%d:%02d maximum)\n",
  150.              MAX_IDLE / 60, MAX_IDLE % 60);
  151.     }
  152.     if (criteria.changeMask & AVAIL_SWAP) {
  153.         fprintf (stderr, "\tswap percentage too high (%d%% maximum)\n",
  154.              MAX_SWAP);
  155.     }
  156.     if (criteria.changeMask & AVAIL_LOAD) {
  157.         fprintf (stderr, "\tload average too low (%f minimum)\n",
  158.              (double) MIN_LOAD / LOADSCALE);
  159.     }
  160.     if (criteria.changeMask & AVAIL_IMPORTS) {
  161.         fprintf (stderr, "\tjob imports too low (%d minimum)\n",
  162.              MIN_IMPORTS);
  163.     }
  164.     }
  165.     
  166.     printf ("Current criteria:\n");
  167.     if (criteria.idleTime) {
  168.     printf ("\t      idle time:  %d:%02d\n", criteria.idleTime / 60,
  169.         criteria.idleTime % 60);
  170.     }
  171.     if (criteria.swapPct) {
  172.     printf ("\tswap percentage:  %d%%\n", criteria.swapPct);
  173.     }
  174.     if (criteria.loadAvg) {
  175.     printf ("\t   load average:  %f\n",(double)criteria.loadAvg/LOADSCALE);
  176.     }
  177.     if (criteria.imports) {
  178.     printf ("\t  imported jobs:  %d\n", criteria.imports);
  179.     }
  180. }
  181.              
  182.